package com.example.demo.project.utils;

import com.example.demo.project.sys.SysCommon;
import com.sun.deploy.util.StringUtils;

import ognl.Ognl;
import ognl.OgnlException;

import javax.persistence.Column;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.Map.Entry;


public class MsSqlUtils {
	
	public static StringBuilder andLike(StringBuilder sb, List<Object> params, String alias, String columnName, Object columnValue) {
		alias = (null != alias) ? alias.trim() : "";
		String key = columnName;
		Object val = columnValue;
		if (StringUtil.isMeaningFul(key) && null != val) {
			String value = ((String)val).trim();
			value = MsSqlUtils.sqlserverLikeParamHandle(value);
			if (StringUtil.isMeaningFul(value)) {
				if (StringUtil.isMeaningFul(alias)) {
					sb.append(" and ").append(alias).append(".").append(key).append(" like ?");
				} else {
					sb.append(" and ").append(key).append(" like ?");
				}
				params.add("%" + value + "%");
			}
		}
		return sb;
	}
	
	public static StringBuilder andLike2(StringBuilder sb, List<Object> params, String alias, Object ognlRoot, String ognlExp) {
		try {
			alias = (null != alias) ? alias.trim() : "";
			Object val = Ognl.getValue(ognlExp, ognlRoot);
			andLike(sb, params, alias, ognlExp, val);
		} catch (OgnlException o) {
			throw new IllegalStateException("没有找到" + ognlExp);
		}
		return sb;
	}
	
	public static StringBuilder andLikeBatch(StringBuilder sb, List<Object> params, String alias, Map<String, Object> cvMap) {
		alias = (null != alias) ? alias.trim() : "";
		Set<Entry<String, Object>> set = cvMap.entrySet();
		for (Entry<String, Object> o : set) {
			String key = o.getKey();
			Object val = o.getValue();
			if (StringUtil.isMeaningFul(key) && null != val) {
				String value = ((String)val).trim();
				if (StringUtil.isMeaningFul(value)) {
					sb.append(" and ").append(alias).append(".").append(key).append(" like ?");
					params.add("%" + value + "%");
				}
			}
		}
		return sb;
	}

	public static StringBuilder andLikeBatch(StringBuilder sb, List<Object> params, String alias, Object ognlRoot, String...ognlExps) {
		if (null != ognlExps) {
			for (String columnName : ognlExps) {
				if (StringUtil.isMeaningFul(columnName)) {
					andLike2(sb, params, alias, ognlRoot, columnName);
				}
			}
		}
		return sb;
	}
	
	public static StringBuilder andEqual(StringBuilder sb, List<Object> params, String alias, String columnName, Object columnValue) {
		if (null != columnValue) {
			alias = (null != alias) ? alias.trim() : "";
			if (columnValue.getClass() == String.class) {
				columnValue = ((String)columnValue).trim();
				if (columnValue.equals("")) return sb;
			} 
			if (StringUtil.isMeaningFul(alias)) {
				sb.append(" and ").append(alias).append(".").append(columnName).append(" = ?");
			} else {
				sb.append(" and ").append(columnName).append(" = ?");
			}
			params.add(columnValue);
		}
		return sb;
	}
	
	public static StringBuilder andMoreThan(StringBuilder sb, List<Object> params, String alias, String columnName, Object columnValue) {
		alias = (null != alias) ? alias.trim() : "";
		if (null != columnValue) {
			if (columnValue.getClass() == String.class) {
				columnValue = ((String)columnValue).trim();
			} 
			sb.append(" and ").append(alias).append(".").append(columnName).append(" > ?");
			params.add(columnValue);
		}
		return sb;
	}
	
	public static StringBuilder andMoreThanEqual(StringBuilder sb, List<Object> params, String alias, String columnName, Object columnValue) {
		alias = (null != alias) ? alias.trim() : "";
		if (null != columnValue) {
			if (columnValue.getClass() == String.class) {
				columnValue = ((String)columnValue).trim();
			} 
			sb.append(" and ");
			if (StringUtil.isMeaningFul(alias)) {
				sb.append(alias).append(".");
			}
			sb.append(columnName).append(" >= ?");
			params.add(columnValue);
		}
		return sb;
	}
	
	public static StringBuilder andLessThan(StringBuilder sb, List<Object> params, String alias, String columnName, Object columnValue) {
		alias = (null != alias) ? alias.trim() : "";
		if (null != columnValue) {
			if (columnValue.getClass() == String.class) {
				columnValue = ((String)columnValue).trim();
			} 
			sb.append(" and ");
			if (StringUtil.isMeaningFul(alias)) {
				sb.append(alias).append(".");
			}
			sb.append(columnName).append(" < ?");
			params.add(columnValue);
		}
		return sb;
	}
	
	public static StringBuilder andLessThanEqual(StringBuilder sb, List<Object> params, String alias, String columnName, Object columnValue) {
		alias = (null != alias) ? alias.trim() : "";
		if (null != columnValue) {
			if (columnValue.getClass() == String.class) {
				columnValue = ((String)columnValue).trim();
			} 
			sb.append(" and ").append(alias).append(".").append(columnName).append(" <= ?");
			params.add(columnValue);
		}
		return sb;
	}
	
	public static StringBuilder andEqual2(StringBuilder sb, List<Object> params, String alias, Object ognlRoot, String ognlExp) {
		try {
			alias = (null != alias) ? alias.trim() : "";
			Object val = Ognl.getValue(ognlExp, ognlRoot);
			andEqual(sb, params, alias, ognlExp, val);
		} catch (OgnlException o) {
			throw new IllegalStateException("没有找到" + ognlExp);
		}
		return sb;
	}
	
	public static StringBuilder andEqual(StringBuilder sb, List<Object> params, String alias, Map<String, Object> cvMap) {
		alias = (null != alias) ? alias.trim() : "";
		Set<Entry<String, Object>> set = cvMap.entrySet();
		for (Entry<String, Object> o : set) {
			String key = o.getKey();
			Object val = o.getValue();
			if (StringUtil.isMeaningFul(key) && null != val) {
				if (val.getClass() == String.class) {
					val = ((String) val).trim();
				}
				sb.append(" and ").append(alias).append(".").append(key).append(" = ?");
				params.add(val);
			}
		}
		return sb;
	}
	
	public static StringBuilder andEqualBatch(StringBuilder sb, List<Object> params, String alias, Object ognlRoot, String...ognlExps) {
		if (null != ognlExps) {
			for (String columnName : ognlExps) {
				if (StringUtil.isMeaningFul(columnName)) {
					andEqual2(sb, params, alias, ognlRoot, columnName);
				}
			}
		}
		return sb;
	}

	public static StringBuilder andDateRange(StringBuilder sb, List<Object> params, String alias, String columnName, Date startDate, Date endDate) {
		if (null != startDate) {
			andMoreThanEqual(sb, params, alias, columnName, DateUtils.dateOnly(startDate).getTime());
		}
		if (null != endDate) {
			andLessThan(sb, params, alias, columnName, DateUtils.tomorrow(endDate).getTime());
		}
		return sb;
	}

	public static StringBuilder andDateRange(StringBuilder sb, List<Object> params, String alias, String columnName, String startDateStr, String endDateStr) {
		Date startDate = null;
		if (StringUtil.isMeaningFul(startDateStr)) {
			startDate = SysCommon.YMD.parse(startDateStr);
		}
		Date endDate = null;
		if (StringUtil.isMeaningFul(endDateStr)) {
			endDate = SysCommon.YMD.parse(endDateStr);
		}
		return andDateRange(sb, params, alias, columnName, startDate, endDate);
	}

	public static StringBuilder andDateRange(StringBuilder sb, List<Object> params, String alias, String columnName, Object ognlRoot, String ognlExp) {
		try {
			alias = (null != alias) ? alias.trim() : "";
			Date startDate = (Date) Ognl.getValue(ognlExp + "Start", ognlRoot);
			Date endDate = (Date) Ognl.getValue(ognlExp + "End", ognlRoot);
			return andDateRange(sb, params, alias, columnName, startDate, endDate);
		} catch (OgnlException o) {
			throw new IllegalStateException("没有找到" + ognlExp);
		}
	}
	
	public static StringBuilder andDateRange(StringBuilder sb, List<Object> params, String alias, Object ognlRoot, String ognlExp) {
		return andDateRange(sb, params, alias, ognlExp, ognlRoot, ognlExp);
	}
	

	

	
	/**
	 * sqlserver like语法查询时处理字符串。
	 * @param value
	 * @return
	 */
	public static String sqlserverLikeParamHandle(String value) {
		return null == value ? "" : value
			.replaceAll("\\[", "[[]")
			.replaceAll("%", "[%]")
			.replaceAll("_", "[_]")
			.replaceAll("\\^", "[^]");
	}
	




/*	public static StringBuilder andEqualDepartment(StringBuilder sb, List<Object> params, String alias, String columnName, String departmentId,Account account, String companyId) {
		if(account==null) return sb;
		if(!StringUtil.isMeaningFul(companyId))return sb;
		Boolean isCompanyLeader =  account.getFunctions().contains(new Function(companyId));
		return andEqualDepartment(sb,params,alias,columnName,departmentId,isCompanyLeader);
	}*/

	public static Map<String,String> getFields(Class<?> c) throws NoSuchMethodException, SecurityException {
		Map<String, String> map = new HashMap<>();
		Field[] fields = c.getDeclaredFields();
		for(Field f:fields){
			Method get = c.getMethod("");
			Column column = f.getAnnotation(Column.class);
		}
		return map;
	}
	public static String getMethodName(String field, String type){
		String first = field.substring(0, 1).toUpperCase();
		return type + first + field.substring(1, field.length());
	}
	public static void main(String[] args) {
		System.out.println(getMethodName("businessRanage", "set"));
	}
	
}
